/*
 * Copyright (c) Jack魏 2022 - 2022, All Rights Reserved.
 */

package cn.jack1996.yiyi.controller.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.jack1996.yiyi.bean.SysUser;
import cn.jack1996.yiyi.service.sys.SysUserService;
import cn.jack1996.yiyi.util.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;


/**
 * 系统用户Controller控制类
 *
 * @author Jack魏
 * @since 2022-11-12
 */
@Tag(name = "系统用户接口", description = "系统用户接口")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 注册后台系统用户
     *
     * @param sysUser 前端注册信息
     * @return 注册用户信息
     */
    @Operation(summary = "注册接口", description = "输入用户信息进行注册")
    @Parameter(name = "account", description = "登录账号", required = true)
    @Parameter(name = "password", description = "密码", required = true)
    @PostMapping("/register")
    public Result<String> register(SysUser sysUser) {
        // 登录名参数校验
        if (sysUser.getAccount() == null || "".equals(sysUser.getAccount())) {
            return Result.fail("注册用户名不能为空");
        }
        // 密码参数校验
        if (sysUser.getPassword() == null || "".equals(sysUser.getPassword())) {
            return Result.fail("密码不能为空");
        }

        SysUser user = sysUserService.register(sysUser);
        if (user == null) {
            return Result.fail("注册失败！请更换登录名！");
        }
        return Result.success("注册成功，欢迎您：" + sysUser.getAccount());
    }

    /**
     * 返回用户详情
     *
     * @return 用户详细信息
     */
    @Operation(summary = "用户信息", description = "返回用户信息")
    @GetMapping("/info")
    public Result<SysUser> info() {
        // 这里是从全局解析里面拿的数据
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return Result.success(user);
    }
}
